/**
 * Created by junhong_test on 16/6/20.
 */
define(function () {

    require('../../less/scrollBottom.less');
    
    var ScrollBottom = function () {
        this.hasNext = true;
        this.isLoading = false;
        this.$container = null;
    };

    ScrollBottom.prototype = {
        scrollGetMore:function (container, padding) {
            var _this = this;
            _this.scrollTop = 0;
            this.$container = $(container);

            this.removeScrollBottom();

            this.$container.on('scroll', function (e) {
                //console.log($(e.currentTarget).parent().attr('id')+'内容高度')
                //console.log(e.currentTarget.scrollHeight);
                //console.log($(e.currentTarget).parent().attr('id')+'滚动高度')
                //console.log(e.currentTarget.scrollTop);
                //console.log(_this.scrollTop);

                var scrollHeight = e.currentTarget.scrollHeight;
                var clientHeight = e.currentTarget.clientHeight;
                var scrollTop = e.currentTarget.scrollTop;

                if((scrollTop + clientHeight + padding >= scrollHeight) && (_this.scrollTop <= scrollTop)) {
                    //_this.scrollTop = scrollTop;
                    _this.$container.trigger('scrollBottom', e);
                }
                _this.scrollTop=scrollTop;

            });
        },
        scrollToBottom: function () {
            if (this.$container && this.$container.find('.scroll-bottom').length === 0) {
                this.$container.append('<div class="scroll-bottom">没有更多了</div>');
                this.bindEvent();
            }
        },
        bindEvent: function () {
            var _this = this;
            // this.$container.prev('.HeadView').find('.left').click(function () {
            //     _this.removeScrollBottom();
            // });
            this.$container.closest('.PageView').find('.headview .left').click(function () {
               _this.removeScrollBottom();
            });
        },
        removeScrollBottom: function () {
            if (this.$container.find('.scroll-bottom').length > 0) {
                this.$container.find('.scroll-bottom').remove();
            }
        }
    };
   
    return ScrollBottom;
});